﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

namespace QuanLySuKien.model
{
    public sealed class ThongTinKHDAC
    {
        public static SqlDataReader GetKhachHangInfomation(string filter)
        {
            SqlDataReader reader;
            string sql = "SELECT * FROM ThongTinKH WHERE HoTen LIKE @prmHoTen";
            
            using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
            {
                command.Parameters.Add("@prmHoTen", SqlDbType.NVarChar, 150).Value = filter;
                reader = command.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.CloseConnection);
            }
            return reader;
        }
        public static bool InsertKhachHang(string UserName, string HoTen, string NgaySinh, string SoDT, string Email, string DiaChi, string MoTa)
        {
            bool success = false;

            string sql = " INSERT INTO ThongTinKH (UserName, HoTen, NgaySinh, SoDT, Email, DiaChi, MoTa ) " +
                "VALUES (@prmUserName, @prmHoTen, @prmNgaySinh, @prmSoDT, @prmEmail, @prmDiaChi, @prmMoTa)";
            using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
            {
                //command.Parameters.Add("@prmUserId", SqlDbType.UniqueIdentifier).Value = UserId;
                command.Parameters.Add("@prmUserName", SqlDbType.NVarChar, 256).Value = UserName;
                command.Parameters.Add("@prmHoTen", SqlDbType.NVarChar, 100).Value = HoTen;
                command.Parameters.Add("@prmNgaySinh", SqlDbType.DateTime, 150).Value = NgaySinh;
                command.Parameters.Add("@prmSoDT", SqlDbType.NVarChar, 12).Value = SoDT;
                command.Parameters.Add("@prmEmail", SqlDbType.NVarChar, 50).Value = Email;
                command.Parameters.Add("@prmDiaChi", SqlDbType.NVarChar, 100).Value = DiaChi;
                command.Parameters.Add("@prmMoTa", SqlDbType.NVarChar,200).Value = MoTa;
                int rowAffected = command.ExecuteNonQuery();
                success = (rowAffected == 1);

            }
            return success;
        }
        public static bool UdateKhachHang(string UserId, string HoTen, string NgaySinh, string SoDT, string Email, string DiaChi, string MoTa)
        {
            bool success = false;
            string sql = "UPDATE ThongTinKH SET  HoTen=@prmHoTen, " +
                                                "NgaySinh=@prmNgaySinh " +
                                                "SoDT=@prmSoDT " +
                                                "Email=@prmEmail " +
                                                "DiaChi=@prmDiaChi " +
                                                "MoTa=@prmMoTa " +
                                                "WHERE UserId=@prmUserId";
            using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
            {
                //command.Parameters.Add("@prmUserId", SqlDbType.UniqueIdentifier).Value = UserId;
                command.Parameters.Add("@prmHoTen", SqlDbType.NVarChar, 100).Value = HoTen;
                command.Parameters.Add("@prmNgaySinh", SqlDbType.DateTime, 150).Value = NgaySinh;
                command.Parameters.Add("@prmSoDT", SqlDbType.NVarChar, 12).Value = SoDT;
                command.Parameters.Add("@prmEmail", SqlDbType.NVarChar, 50).Value = Email;
                command.Parameters.Add("@prmDiaChi", SqlDbType.NVarChar, 100).Value = DiaChi;
                command.Parameters.Add("@prmMoTa", SqlDbType.NVarChar, 200).Value = MoTa;
                int rowAffected = command.ExecuteNonQuery();
                success = (rowAffected == 1);

            }
            return success;
        }
        public static bool DeleteThongTinKH(String UserId)
        {
            bool success = false;
            String sql = "DELETE FROM ThongTinKH WHERE UserId=@prmUserId";
            using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
            {
                //command.Parameters.Add("@prmUserId", SqlDbType.UniqueIdentifier).Value = UserId;
                int rowAffected = command.ExecuteNonQuery();
                success = (rowAffected == 1);

            }
            return success;
        }

    }

}